//
//  YMLoginView.swift
//  YMSwiftUI
//
//  Created by anscen on 2022/3/29.
//

import SwiftUI
import ActivityIndicatorView
import ToastUI
import SwiftUIX

struct YMLoginView: View {
    @EnvironmentObject var client :YMClient
    @Binding var isLogin :Bool
    @State private var account :String = ""
    @State private var pwd     :String = ""
    @State private var isVisible :Bool = false
    @State var hudVisible = true
    var body: some View {
        GeometryReader { root in
            VStack(alignment: .leading, spacing: 0) {
                HStack(alignment: .center, spacing: 0) {
                    Button {
                        self.isLogin = false
                    } label: {
                        HStack {
                            Image("icon_nav_back").frame(width: 44, height: 44, alignment: .leading)
                        }
                    }.offset(x: 15, y: 0)
                    Text("账号登录").font(Font(UIFont.boldSystemFont(ofSize: 20))).foregroundColor(.black).frame(width: root.size.width - 30 - 88, height: 44, alignment: .center)
                }.padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
                
                ScrollView.init {
                    VStack.init(alignment: .center, spacing: 0) {
                        HStack(alignment: .center, spacing: 15) {
                            Text("账 号:").font(Font.system(size: 15)).foregroundColor(Color.appx333333())
                            TextField("请输入用户名", text: $account).textFieldStyle(.roundedBorder).frame(height:40).onChange(of: account) { value in
                                debugPrint(value)
                            }
                        }.padding(EdgeInsets(top: 50, leading: 15, bottom: 0, trailing: 15))
                        
                        HStack(alignment: .center, spacing: 15) {
                            Text("密 码:").font(Font.system(size: 15)).foregroundColor(Color.appx333333())
                            SecureField("请输入密码", text: $pwd).textFieldStyle(.roundedBorder).frame(height:40).onChange(of: pwd) { value in
                                debugPrint(value)
                            }
                        }.padding(EdgeInsets(top: 30, leading: 15, bottom: 20, trailing: 15))
                        Button {
                            if account.count > 0 && pwd.count > 0 {
                                self.loginAction()
                            }
                        } label: {
                            Text("登录").font(Font.system(size: 16)).foregroundColor(Color.white).frame(width: root.size.width - 30, height: 45, alignment: .center)
                        }.background((account.count > 0 && pwd.count > 0) ? AppColor : AppXDDDDDD).cornerRadius(5).padding().toast(isPresented: $isVisible) {
                        } content: {
                            ToastView("加载中...").toastViewStyle(IndefiniteProgressToastViewStyle())
                        }
                    }
                }
            }
        }
    }
    func loginAction(){
        self.isVisible = true
        ApiMoya.request(target: .classify) { json in
            let user = YMUser()
            user.name = account.count > 0 ? self.account : "战神吕布"
            user.userId = "10086"
            user.corver = "http://img.1391.com/api/v1/bookcenter/cover/1/175247/175247_4da137ef840342c8ac06d159fd96c0c9.jpg"
            YMUser.saveUser(user)
            self.isVisible = false
            self.isLogin = false
        } failure: { error in
            self.isVisible = false
        }
    }
}
